﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using BusinessObjects;
using YourWorld.Common.Infrastucture.Events;
using System.Windows;
using Microsoft.Practices.Composite.Regions;
using YourWorld.Common;
using Microsoft.Practices.Unity;
using LogInModule.Views;

namespace YourWorld
{
    public partial class ShellView : UserControl
    {
        private Point lastLocation;
        private bool drag = false;
        private Point lastDragPoint;
        private double currentScale = 1;

        private int sleep = Consts.SleepBetweenPlayerMoves;
        private DateTime lastMoveTime = DateTime.Now;

        private bool isLeftDown = false, isRightDown = false, isUpDown = false, isDownDown = false;

        public ShellView()
        {
            InitializeComponent();

            this.LayoutRoot.KeyUp += new KeyEventHandler( LayoutRoot_KeyUp );
        }

        private void LayoutRoot_KeyUp( object sender, KeyEventArgs e )
        {
            switch ( e.Key )
            {
                case Key.Left: isLeftDown = false; break;
                case Key.Right: isRightDown = false; break;
                case Key.Up: isUpDown = false; break;
                case Key.Down: isDownDown = false; break;
            }
        }

        private void LayoutRoot_KeyDown( object sender, KeyEventArgs e )
        {
            switch ( e.Key )
            {
                case Key.Left: isLeftDown = true; isRightDown = false; break;
                case Key.Right: isRightDown = true; isLeftDown = false; break;
                case Key.Up: isUpDown = true; isDownDown = false; break;
                case Key.Down: isDownDown = true; isUpDown = false; break;
            }

            if ( DateTime.Now.Subtract( this.lastMoveTime ).TotalMilliseconds < sleep )
                return;

            this.lastMoveTime = DateTime.Now;

            SimplePoint point = new SimplePoint();
            if ( this.isDownDown == true ) point.Y -= Consts.PlayerStep;
            if ( this.isUpDown == true ) point.Y += Consts.PlayerStep;
            if ( this.isLeftDown == true ) point.X -= Consts.PlayerStep;
            if ( this.isRightDown == true ) point.X += Consts.PlayerStep;


            if ( point.X != 0 || point.Y != 0 )
                EventService.Aggregator.GetEvent<MoveEvent>().Publish( point );
        }

        private void LayoutRoot_MouseWheel( object sender, MouseWheelEventArgs e )
        {
            //if ( e.Delta > 0 )
            //    this.currentScale += 0.05;
            //else if ( this.currentScale > 0 )
            //    this.currentScale -= 0.05;

            //if ( this.currentScale <= 1 && this.currentScale >= 0.4 )
            //{
            //    EventService.Aggregator.GetEvent<ScaleGameMapEvent>().Publish( this.currentScale );
            //}
        }

        private void LayoutRoot_MouseMove( object sender, MouseEventArgs e )
        {
            //if ( drag )
            //{
            //    Point curDragPoint = e.GetPosition( this.Host );

            //    double x = (curDragPoint.X - lastDragPoint.X);
            //    double y = (curDragPoint.Y - lastDragPoint.Y);
            //    EventService.Aggregator.GetEvent<ScrollWithOffsetEvent>().Publish( new Point( x, y ) );

            //    lastDragPoint = curDragPoint;
            //}
        }

        private void LayoutRoot_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
        {
            //this.drag = false;
            //this.Cursor = Cursors.Arrow;
        }

        private void LayoutRoot_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            //    this.drag = true;
            //    this.lastDragPoint = e.GetPosition( this.Host );
            //    this.Cursor = Cursors.Hand;
        }
    }
}
